package com.wxy.channel;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author 47868
 */
public class ChannelTest {
    public static void main(String[] args) {
        try (FileChannel channel = new FileInputStream("test1.txt").getChannel()) {
            //准备缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(10);
            while (channel.read(buffer) != -1) {
                buffer.flip();//切换至读模式
                while (buffer.hasRemaining()) {
                    System.out.println((char) buffer.get());
                }
                buffer.clear(); //切换至写模式
            }
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}
